﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using yq.IBase.DBHelper;
using yq.CacheHelp;
using yq.IBase.Basis;
using yq.IBase.Cache;

namespace yq.IBase
{
    /// <summary>
    /// 数据库执行操作委托
    /// 如果涉及缓存同步请重写
    /// </summary>
    public class DbExec
    {
        /// <summary>
        /// 执行查询（读库）
        /// </summary>
        /// <typeparam name="F"></typeparam>
        /// <param name="doFunc"></param>
        /// <returns></returns>
        public static F DoRead<F>(Func<DbHelper, F> doFunc, LogMsg log = null)
        {
            DateTime openTime = DateTime.Now;
            using (DbHelper helper = new DbHelper())
            {
                try
                {
                    var val = doFunc(helper);
                    CacheManage.Log(log, helper.SqlLog, DateTime.Now - openTime);
                    return val;
                }
                catch (Exception ex)
                {
                    //日志
                    CacheManage.Log(log, helper.SqlLog, DateTime.Now - openTime, ex.Message.ToString());
                    throw;
                }
            }
        }

        /// <summary>
        /// 执行SQL语句（开启事务，操作写库，无返回值）
        /// </summary>
        /// <param name="doFunc"></param>
        public static void DoTrans(Action<DbHelper, CaCheRefresh> doFunc, LogMsg log = null)
        {
            DateTime openTime = DateTime.Now;
            using (DBTrans ts = new DBTrans())
            {
                DbHelper helper = new DbHelper(ts);
                try
                {
                    doFunc(helper, helper.CaCheRefresh);

                    //防止未提交事物引起的异常
                    if (helper.TransType == TransType.Open)
                    {
                        helper.Commit();
                    }
                    CacheManage.Log(log, helper.SqlLog, DateTime.Now - openTime);
                }
                catch (Exception ex)
                {
                    helper.Rollback();

                    //日志
                    CacheManage.Log(log, helper.SqlLog, DateTime.Now - openTime, ex.Message.ToString());
                    throw;
                }
                finally
                {
                    CacheManage.Refresh(helper.CaCheRefresh);
                }
            }
        }

        /// <summary>
        /// 执行SQL语句（开启事务，操作写库）
        /// </summary>
        /// <typeparam name="F"></typeparam>
        /// <param name="dofunc"></param>
        /// <returns></returns>
        public static F DoTrans<F>(Func<DbHelper, CaCheRefresh, F> doFunc, LogMsg log = null)
        {
            DateTime openTime = DateTime.Now;
            using (DBTrans ts = new DBTrans())
            {
                DbHelper helper = new DbHelper(ts);
                try
                {
                    var outval = doFunc(helper, helper.CaCheRefresh);

                    //防止未提交事物引起的异常
                    if (helper.TransType == TransType.Open)
                    {
                        helper.Commit();
                    }
                    CacheManage.Log(log, helper.SqlLog, DateTime.Now - openTime);
                    return outval;
                }
                catch (Exception ex)
                {
                    helper.Rollback();

                    //日志
                    CacheManage.Log(log,helper.SqlLog, DateTime.Now - openTime, ex.Message.ToString());
                    throw;
                }
                finally {
                    CacheManage.Refresh(helper.CaCheRefresh);
                }

            }
        }

        /// <summary>
        /// 执行SQL语句（不开启事务，操作写库）
        /// </summary>
        /// <typeparam name="F"></typeparam>
        /// <param name="doFunc"></param>
        /// <returns></returns>
        public static F DoExecute<F>(Func<DbHelper, CaCheRefresh, F> doFunc,LogMsg log=null)
        {
            DateTime openTime = DateTime.Now;
            using (DbHelper helper = new DbHelper(DbHelper.DbHelperConnectionWrite))
            {
                try
                {
                    var outval = doFunc(helper, helper.CaCheRefresh);
                    CacheManage.Log(log,helper.SqlLog, DateTime.Now - openTime);
                    return outval;
                }
                catch (Exception ex)
                {
                    //日志
                    CacheManage.Log(log,helper.SqlLog, DateTime.Now - openTime, ex.Message.ToString());
                    throw;
                }
                finally
                {
                    CacheManage.Refresh(helper.CaCheRefresh);
                }
            }
        }

    }
}
